package com.study.controller;

import com.study.pojo.Video;
import com.study.pojo.VideoOrder;
import com.study.service.OrderService;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/v1/video_order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/save")
    public Object save(int videoId){
        //访问视频服务
        Video video = orderService.findById(videoId);
        //构造返回对象
        VideoOrder videoOrder = new VideoOrder();
        videoOrder.setVideoId(videoId);
        videoOrder.setVideoTitle(video.getTitle());
        videoOrder.setCreateTime(video.getCreateTime());
        return videoOrder;
    }

    //此处为了方便测试，是get请求
    @GetMapping("/insert")
    public void insert(){
        //此处为了测试方便，直接查询了一个视频
        Video video = orderService.findById(40);
        //远程调用
        orderService.insert(video);
    }

    @GetMapping("/threadTest")
    public Object threadTest(HttpServletRequest req){
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("title", "SpringCloudAlibaba微服务");
        map.put("port", req.getServerPort());
        return map;
    }

}
